<template>
	<view class="weihu-bg">
		<image class="weihu-bg-img" src="https://yun.qiongtuyule.com/static/img/weihu/bg.png" mode="aspectFill"></image>
		<view class="weihu-content">
			<view class="weihu-title">设备维护</view>

			<!-- 筛选区 -->
			<view class="weihu-filter-row">
				<picker :range="projectList" @change="onProjectChange">
					<view class="weihu-filter-select">
						<text>{{ projectList[projectIndex] }}</text>
						<image src="/static/img/weihu/jiantou.png" class="weihu-select-arrow"></image>
					</view>
				</picker>

				<picker mode="multiSelector" :range="regionMultiArray" :value="regionMultiIndex"
					@change="onRegionChange" @columnchange="onRegionColumnChange">
					<view class="weihu-filter-select">
						<text>{{ regionMultiArray[1][regionMultiIndex[1]] }}</text>
						<image src="/static/img/weihu/jiantou.png" class="weihu-select-arrow"></image>
					</view>
				</picker>

				<picker :range="dateList" @change="onDateChange">
					<view class="weihu-filter-select">
						<text>{{ dateList[dateIndex] }}</text>
						<image src="/static/img/weihu/jiantou.png" class="weihu-select-arrow"></image>
					</view>
				</picker>
			</view>

			<view class="weihu-stat-card">
				<text class="weihu-stat-title">今日异常设备</text>
				<text class="weihu-stat-num">{{ abnormalList.length }}</text>
			</view>

			<view v-for="item in abnormalList" :key="item.id" class="weihu-device-card" @click="goProjectDetail(item)">
				<view class="weihu-device-row">
					<view>
						<text class="weihu-device-title">{{ item.project }}</text>
						<text class="weihu-device-region">/{{ item.region }}</text>
					</view>
					<view class="weihu-device-alarm">
						<image src="/static/img/weihu/jingbao.png" class="weihu-alarm-icon"></image>
						<text class="weihu-alarm-text">异常</text>
					</view>
				</view>
				<view class="weihu-device-info">
					<text>电量：{{ item.battery }}%</text>
					<text class="weihu-info-gap">信号强度：{{ item.signal }}%</text>
				</view>
				<view class="weihu-device-info">
					<text>设备编号：{{ item.code }}</text>
				</view>
				<view class="weihu-device-info">
					<text>SIM卡号：{{ item.pn }}</text>
				</view>
				<view class="weihu-device-info">
					<text>更新时间：{{ item.time }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				projectList: ['全部项目'],
				projectOptions: [],
				projectIndex: 0,
				selectedProjectId: 0,
				dateList: ['今日', '昨日', '本周', '本月', '全部'],
				dateIndex: 0,
				abnormalList: [],

				regionMultiArray: [
					[],
					[]
				],
				regionMultiIndex: [0, 0],
				rawRegionData: [],
				provinceId: 0,
				cityId: 0
			};
		},
		computed: {
			...mapState([
				"userInfo", "location"
			]),
		},
		onShow() {
			if (this.userInfo.token == undefined) {
				this.loginOut();
				return
			}
			this.getStatusBarHeight();
			this.loadRegionData();
			this.fetchAbnormalList();
			this.getSiteList();
		},
		methods: {
			async getSiteList() {
				let res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.siteTree3,
					data: {
						page: 1,
						limit: 100
					}
				})
				if (res.code === 1 && Array.isArray(res.data)) {
					this.projectOptions = res.data;
					this.projectList = ['全部项目', ...res.data.map(item => item.text)];
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			getStatusBarHeight() {
				const systemInfo = uni.getSystemInfoSync();
				this.statusBarHeight = systemInfo.statusBarHeight;
			},
			async loadRegionData() {
				const res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.siteTree,
					needLogin: true
				});
				if (res.code === 1) {
					this.rawRegionData = res.data;

					const provinces = ['全部', ...res.data.map(item => item.text)];
					const cities = ['全部']; // 初始为“全部”

					this.regionMultiArray = [provinces, cities];
					this.regionMultiIndex = [0, 0];

					// 默认选中全部
					this.provinceId = 0;
					this.cityId = 0;
				}
			},
			onProjectChange(e) {
				this.projectIndex = e.detail.value;
				this.selectedProjectId = this.projectIndex === 0 ? 0 : this.projectOptions[this.projectIndex - 1].value;
				// alert(this.selectedProjectId)
				this.fetchAbnormalList();
			},
			onDateChange(e) {
				this.dateIndex = e.detail.value;
				this.fetchAbnormalList();
			},
			onRegionColumnChange(e) {
				const column = e.detail.column;
				const value = e.detail.value;

				if (column === 0) {
					this.regionMultiIndex[0] = value;
					this.regionMultiIndex[1] = 0;

					if (value === 0) {
						// 选择了“全部省份”
						this.regionMultiArray[1] = ['全部'];
					} else {
						const selectedProvince = this.rawRegionData[value - 1]; // -1 因为前面加了“全部”
						const cities = selectedProvince.children.map(item => item.text);
						this.regionMultiArray[1] = ['全部', ...cities];
					}
				}
			},
			onRegionChange(e) {
				const [provinceIdx, cityIdx] = e.detail.value;
				this.regionMultiIndex = [provinceIdx, cityIdx];

				if (provinceIdx === 0) {
					// 全部省、市
					this.provinceId = 0;
					this.cityId = 0;
				} else {
					const selectedProvince = this.rawRegionData[provinceIdx - 1];
					this.provinceId = selectedProvince.id;

					if (cityIdx === 0) {
						this.cityId = 0; // 全部城市
					} else {
						const selectedCity = selectedProvince.children[cityIdx - 1]; // -1 因为加了“全部”
						this.cityId = selectedCity ? selectedCity.id : 0;
					}
				}

				this.fetchAbnormalList();
			},
			formatDate(timestamp) {
				const date = new Date(timestamp*1000);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const hours = date.getHours().toString().padStart(2, '0');
				const minutes = date.getMinutes().toString().padStart(2, '0');
				const seconds = date.getSeconds().toString().padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			async fetchAbnormalList() {
				const res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.abnormalList,
					data: {
						project_id: this.selectedProjectId === 0 ? 0 : this.selectedProjectId,
						province_id: this.provinceId,
						city_id: this.cityId,
						date_type: this.dateIndex
					},
					needLogin: true
				});
				if (res.code === 1 && Array.isArray(res.data)) {
					this.abnormalList = res.data.map(item => ({
						id: item.id,
						project: item.depart_name,
						region: `${item.area_name2 || ''}${item.area_name1 || ''}`,
						battery: item.battery,
						signal: item.signal_strength,
						code: item.code,
						pn:item.pn,
						time: this.formatDate(item.createtime)
					}));
				} else {
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				}
			},
			goProjectDetail(item) {
				uni.navigateTo({
					url: `/pages/shouye/maintain?id=${item.id}`
				});
			}
		}
	};
</script>
<style>
	.weihu-bg {
		min-height: 100vh;
		background: #e6eaf2;
		position: relative;
		padding-top: 60rpx;
	}

	.weihu-bg-img {
		position: absolute;
		left: 0;
		top: 0;
		width: 100vw;
		height: 100vh;
		z-index: 0;
		object-fit: cover;
	}

	.weihu-content {
		position: relative;
		z-index: 1;
		/* padding-top: 60rpx; */
		/* 内容整体下移 */
	}

	.weihu-title {
		font-size: 44rpx;
		font-weight: bold;
		color: #222;
		margin-top: 32rpx;
		margin-left: 32rpx;
		margin-bottom: 40rpx;
	}

	.weihu-filter-row {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin: 0 32rpx 32rpx 32rpx;
		gap: 0;
	}

	.weihu-filter-select {
		width: 220rpx;
		height: 60rpx;
		background: transparent;
		border: 2rpx solid #000;
		/* 改为纯黑色边框 */
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		/* 左右空间自动分配 */
		font-size: 28rpx;
		color: #000;
		/* 改为纯黑色文字 */
		margin: 0 8rpx;
		position: relative;
		padding: 0 16rpx;
		/* 左右内边距，保证内容不贴边 */
		box-sizing: border-box;
		/* 保证 padding 不影响整体宽度 */
	}

	.weihu-filter-select text {
		flex: 1;
		text-align: left;
	}

	.weihu-select-arrow {
		width: 14rpx;
		height: 14rpx;
		margin-left: 8rpx;
		margin-right: 4rpx;
		/* 给箭头右边留一点空隙 */
	}

	.weihu-stat-card {
		background: #fff;
		border-radius: 18rpx;
		margin: 0 24rpx 32rpx 24rpx;
		padding: 32rpx 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 32rpx;
		font-weight: bold;
		color: #222;
		box-shadow: 0 2rpx 12rpx #e3eaf6;
	}

	.weihu-stat-title {
		font-size: 32rpx;
		font-weight: bold;
	}

	.weihu-stat-num {
		font-size: 36rpx;
		font-weight: bold;
	}

	.weihu-device-card {
		background: #fff;
		border-radius: 18rpx;
		margin: 0 24rpx 32rpx 24rpx;
		padding: 32rpx 28rpx 24rpx 28rpx;
		box-shadow: 0 2rpx 12rpx #e3eaf6;
	}

	.weihu-device-row {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		margin-bottom: 18rpx;
	}

	.weihu-device-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #222;
	}

	.weihu-device-region {
		font-size: 26rpx;
		color: #666;
		margin-left: 8rpx;
	}

	.weihu-device-alarm {
		display: flex;
		align-items: center;
		margin-left: 12rpx;
	}

	.weihu-alarm-icon {
		width: 36rpx;
		height: 36rpx;
		margin-right: 12rpx;
	}

	.weihu-alarm-text {
		color: #f33;
		font-size: 30rpx;
		font-weight: bold;
	}

	.weihu-device-info {
		font-size: 22rpx;
		color: #222;
		margin-top: 16rpx;
		display: flex;
		align-items: center;
	}

	.weihu-info-gap {
		margin-left: 40rpx;
	}
</style>